/*
 * Copyright (C) 2014 pengjianbo(pengjianbosoft@gmail.com), Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.lemon.smartsafety.utils.photo;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.lemon.smartsafety.R;

import cn.finalteam.galleryfinal.widget.GFImageView;

/**
 * Desction:
 * Author:pengjianbo
 * Date:15/12/1 下午10:28
 */
public class GlideImageLoader implements cn.finalteam.galleryfinal.ImageLoader {
    private Context mContext;


    public GlideImageLoader() {

    }

    @Override
    public void displayImage(Activity activity, String path, final GFImageView imageView, Drawable defaultDrawable, int width, int height) {
        DrawableRequestBuilder builder = null;
        String                 url     = path;
        if (url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")) {
            url = path;
            builder = Glide.with(activity).load(url).placeholder(defaultDrawable).error(defaultDrawable).override(width, height).diskCacheStrategy(DiskCacheStrategy.RESULT).skipMemoryCache(false);
        } else {
            url = "file://" + path;
            builder = Glide.with(activity).load(url).placeholder(defaultDrawable).error(defaultDrawable).override(width, height).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true);
        }
        builder.into(new ImageViewTarget<GlideDrawable>(imageView) {
            @Override
            protected void setResource(GlideDrawable resource) {
                imageView.setImageDrawable(resource);
            }

            @Override
            public void setRequest(Request request) {
                imageView.setTag(R.id.adapter_item_tag_key, request);
            }

            @Override
            public Request getRequest() {
                return (Request) imageView.getTag(R.id.adapter_item_tag_key);
            }
        });
    }


    @Override
    public void clearMemoryCache() {
    }

    public static void loadImage(Context context, String path, final ImageView imageView) {//R.mipmap.img_placeholder
        loadImage(context, path, imageView, R.mipmap.img_placeholder);
    }

    public static void loadImage(Context context, String path, final ImageView imageView, int default_image_res) {
        String url = path;
        if (url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")) {
            url = path;
        } else {
            url = "file://" + path;
        }
        Glide.with(context).load(url).placeholder(default_image_res).error(default_image_res)
             //.centerCrop()
             .into(new ImageViewTarget<GlideDrawable>(imageView) {
                 @Override
                 protected void setResource(GlideDrawable resource) {
                     imageView.setImageDrawable(resource);
                 }

                 @Override
                 public void setRequest(Request request) {
                     imageView.setTag(R.id.adapter_item_tag_key, request);
                 }

                 @Override
                 public Request getRequest() {
                     return (Request) imageView.getTag(R.id.adapter_item_tag_key);
                 }
             });
    }

    public static void loadImage(Context context, String path, final ImageView imageView, int width, int height) {
        String url = path;
        if (url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")) {
            url = path;
        } else {
            url = "file://" + path;
        }
        Glide.with(context).load(url).placeholder(R.mipmap.img_placeholder).error(R.mipmap.img_placeholder).override(width, height)
             //.centerCrop()
             .into(new ImageViewTarget<GlideDrawable>(imageView) {
                 @Override
                 protected void setResource(GlideDrawable resource) {
                     imageView.setImageDrawable(resource);
                 }

                 @Override
                 public void setRequest(Request request) {
                     imageView.setTag(R.id.adapter_item_tag_key, request);
                 }

                 @Override
                 public Request getRequest() {
                     return (Request) imageView.getTag(R.id.adapter_item_tag_key);
                 }
             });
    }


}



/*
Glide.with(this)
                                   .load("")
                                   .asBitmap()
                                   .centerCrop()
                                   .into(500, 500)
                                   .get();
 */